package com.xiaochun.jiankangapp.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.gson.reflect.TypeToken
import com.xiaochun.jiankangapp.R
import com.xiaochun.jiankangapp.base.BaseActivity
import com.xiaochun.jiankangapp.databinding.ActivityResultBinding
import com.xiaochun.jiankangapp.databinding.ListItemUploadFileBinding
import com.xiaochun.jiankangapp.extension.loadUrl
import com.xiaochun.jiankangapp.extension.onClick
import com.xiaochun.jiankangapp.extension.showToast
import com.xiaochun.jiankangapp.extension.start
import com.xiaochun.jiankangapp.model.PersonalInfoModel
import com.xiaochun.jiankangapp.model.ReportModel
import com.xiaochun.jiankangapp.network.Request
import com.xiaochun.jiankangapp.viewmodel.BodyInfoViewModel
import com.xiaochun.jiankangapp.viewmodel.SendReportViewModel
import kotlinx.coroutines.launch

class ResultActivity : BaseActivity() {
    lateinit var viewBinding: ActivityResultBinding
    private val reportViewModel: SendReportViewModel by viewModels()
    private val list = mutableListOf<ReportModel>()
    private val adapter: UploadFileAdapter by lazy {
        UploadFileAdapter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = setContentView()
        viewBinding.tabs.let {
            it.addTab(
                viewBinding.tabs.newTab().setContentDescription("基本资料").setText("基本资料")
                    .setId(1)
            )
            it.addTab(
                viewBinding.tabs.newTab().setContentDescription("体检报告单").setText("体检报告单")
                    .setId(2)
            )
        }
        viewBinding.tabs.setOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (tab?.id) {
                    1 -> {
                        viewBinding.root1.root.visibility = View.VISIBLE
                        viewBinding.root2.root.visibility = View.GONE
                    }

                    2 -> {
                        viewBinding.root1.root.visibility = View.GONE
                        viewBinding.root2.root.visibility = View.VISIBLE
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        viewBinding.root1.root.visibility = View.VISIBLE
        viewBinding.root2.root.visibility = View.GONE
        lifecycleScope.launch {
            val r = Request.postSuspend<PersonalInfoModel>("api/material/details")
            if (r.isSuccessful) {
                r.data?.let {
                    setData1(it)
                }
            }
        }
        lifecycleScope.launch {
            val r = reportViewModel.getList()
            if (r.isSuccessful) {
                r.data?.let {
                    list.clear()
                    list.addAll(it)
                    viewBinding.root2.rv.adapter = adapter
                    viewBinding.root2.edit.onClick {
                        start(SendReportActivity::class.java)
                    }
                }
            }
        }
    }

    private fun setData1(personalInfoModel: PersonalInfoModel) {
        viewBinding.root1.sex.text = personalInfoModel.sex?.let {
            when (it) {
                "men" -> {
                    "男"
                }

                else -> {
                    "女"
                }
            }
        }
        viewBinding.root1.age.text = personalInfoModel.birth
        viewBinding.root1.height.text = personalInfoModel.height.toString() + "CM"
        viewBinding.root1.weight.text = personalInfoModel.weight.toString() + "KG"
        viewBinding.root1.name.text = personalInfoModel.name
        viewBinding.root1.mobile.text = personalInfoModel.mobile
        viewBinding.root1.edit.onClick {
            start(InputInfoActivity::class.java)
            finish()
        }
    }

    private inner class UploadFileAdapter : Adapter<UploadFileViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UploadFileViewHolder {
            return UploadFileViewHolder(
                layoutInflater.inflate(
                    R.layout.list_item_upload_file,
                    parent,
                    false
                )
            )
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: UploadFileViewHolder, position: Int) {
            val item = list[position]
            holder.vb.let {
                it.filename.text = item.name
                it.thumbnai.loadUrl(item.content)
                it.root.onClick {
                    ImagePreviewActivity.start(this@ResultActivity, item.content)
                }
                it.remove.onClick {
                    lifecycleScope.launch {
                        showLoading()
                        val r = Request.postSuspend<Any>(
                            "api/report/delete", mapOf("id" to item.id)
                        )
                        hideLoading()
                        if (r.isSuccessful) {
                            list.removeAt(position)
                            notifyDataSetChanged()
                            showToast("删除成功")
                        } else {
                            showToast("删除失败，" + r.msg)
                        }
                    }
                }
            }
        }

    }

    private class UploadFileViewHolder(itemView: View) : ViewHolder(itemView) {
        val vb = ListItemUploadFileBinding.bind(itemView).apply {
            root.setBackgroundColor(Color.parseColor("#F4F4F4"))
        }
    }
}